A for loop provides a way to "iterate" over an "iterator". This is probably best explained with an example: suppose that you run a popular web service, as part of which you collect anonymous usage data on how long each page load (for the same page) takes. This data might come in the form of a "[list]()" looking like this (in milliseconds – these are not actually implausible values for page load times):
load_times = [190, 170, 120, 80, 300, 210, 533, 1012, 50]

It's useful having this data alone, and we can look at these nine items and see roughly how long page loads are taking. To calculate the average (we'll use the mean) you know (or should know) that the mean of a list containing n items (where n is any whole number greater than zero) we use the formula (sum of all the items) / (number of items) (where / means "divide").

In Python, to calculate the length of load_times we use len(load_times). The sum is a bit less obvious (there is actually a sum(load_times) built into the programming language). Let's create a new variable sum_load_times and work from there.

load_times = [190, 170, 120, 80, 300, 210, 533, 1012, 50] sum_load_times = 0

How do we add each item in load_times to sum_load_times? One way is to try something like this:

# add the first item in `load_times` to `sum_load_times` sum_load_times += load_times[0] # add the second item in `load_times` to `sum_load_times` sum_load_times += load_times[1] # add the third item in `load_times` to `sum_load_times` sum_load_times += load_times[2] # and so on...

We quickly run into a problem though. We can continue this all the way up to nine (that's how many items there are in load_times). The problem is that we don't actually know how long load_times will be in advance (have some imagination and assume that load_times is coming from our web pages, rather than me having just invented some numbers).

This is not a new problem for programming languages, and there is quite a neat solution to this: the for loop. In the code above (where we were doing the adding), you might have noticed that we were actually doing the same thing each time: sum_load_times += load_times[<some number here>]. The only thing that was changing was <some number here>. A for loop gives us a way to fill in <some number here>.

for i in range(len(load_times)): sum_load_times += load_times[i]

Here, range(len(load_times)) means "all the numbers between 0 and len(load_times)-1 (this means we will run sum_load_times += load_times[i] on a total of len(load_times) different numbers)."

We can now compute the average:

load_times = [190, 170, 120, 80, 300, 210, 533, 1012, 50] # compute the sum sum_load_times = 0 for i in range(len(load_times)): sum_load_times += load_times[i] average = sum_load_times / len(load_times) print("The average load time for this page is " + str(average) + "ms.")

Python allows you to also run a for loop for each item in a list directly. This means that

for i in range(len(load_times)): sum_load_times += load_times[i]

is equivalent to

for load_time in load_times: sum_load_times += load_time

practice questions

  1. Write a program to the sum of squares of the numbers in the list [1, 2, 3, 4, 5, 6, 7, 8, 9] (hint: to calculate the square of a number in Python do n**2, e.g. two squared would be 2**2 and three squared would be 3**2).

Answers: for loop answers

further reading